<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <title>动态小闹钟</title>
    <style>
      body {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
        background: #f0f0f0;
      }
      canvas {
        background: white;
        border-radius: 50%;
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
      }
    </style>
  </head>
  <body>
    <canvas id="clock" width="200" height="200"></canvas>

    <script>
      const canvas = document.getElementById('clock')
      const ctx = canvas.getContext('2d')
      const radius = canvas.height / 2
      ctx.translate(radius, radius) // 将坐标原点移到画布中心

      function drawClock() {
        drawFace(ctx, radius)
        drawNumbers(ctx, radius)
        drawTime(ctx, radius)
      }

      function drawFace(ctx, radius) {
        // 外圆
        ctx.beginPath()
        ctx.arc(0, 0, radius, 0, 2 * Math.PI)
        ctx.fillStyle = 'white'
        ctx.fill()

        // 外圈渐变
        let grad = ctx.createRadialGradient(0, 0, radius * 0.95, 0, 0, radius * 1.05)
        grad.addColorStop(0, '#333')
        grad.addColorStop(0.5, 'white')
        grad.addColorStop(1, '#333')
        ctx.strokeStyle = grad
        ctx.lineWidth = radius * 0.1
        ctx.stroke()

        // 中心小圆
        ctx.beginPath()
        ctx.arc(0, 0, radius * 0.1, 0, 2 * Math.PI)
        ctx.fillStyle = '#333'
        ctx.fill()
      }

      function drawNumbers(ctx, radius) {
        ctx.font = radius * 0.15 + 'px Arial'
        ctx.textBaseline = 'middle'
        ctx.textAlign = 'center'
        for (let num = 1; num <= 12; num++) {
          let ang = (num * Math.PI) / 6
          ctx.rotate(ang)
          ctx.translate(0, -radius * 0.85)
          ctx.rotate(-ang)
          ctx.fillStyle = '#333'
          ctx.fillText(num.toString(), 0, 0)
          ctx.rotate(ang)
          ctx.translate(0, radius * 0.85)
          ctx.rotate(-ang)
        }
      }

      function drawTime(ctx, radius) {
        const now = new Date()
        let hour = now.getHours()
        let minute = now.getMinutes()
        let second = now.getSeconds()

        // 计算时针角度
        hour = hour % 12
        let hourAngle = (hour * Math.PI) / 6 + (minute * Math.PI) / (6 * 60) + (second * Math.PI) / (360 * 60)
        drawHand(ctx, hourAngle, radius * 0.5, radius * 0.07)

        // 计算分针角度
        let minuteAngle = (minute * Math.PI) / 30 + (second * Math.PI) / (30 * 60)
        drawHand(ctx, minuteAngle, radius * 0.8, radius * 0.07)

        // 计算秒针角度
        let secondAngle = (second * Math.PI) / 30
        drawHand(ctx, secondAngle, radius * 0.9, radius * 0.02, 'red')
      }

      function drawHand(ctx, pos, length, width, color = '#333') {
        ctx.beginPath()
        ctx.lineWidth = width
        ctx.lineCap = 'round'
        ctx.strokeStyle = color
        ctx.moveTo(0, 0)
        ctx.rotate(pos)
        ctx.lineTo(0, -length)
        ctx.stroke()
        ctx.rotate(-pos)
      }

      function update() {
        ctx.clearRect(-radius, -radius, canvas.width, canvas.height)
        drawClock()
        requestAnimationFrame(update)
      }

      update()
    </script>
  </body>
</html>
